var ParseTemplate = ZFT.Util.ParseTemplate;
var STip = ZFT.Util.STip;
var Ajax = ZFT.Util.Ajax;
var Template = {
  count: ParseTemplate(require('./tpl/count.xtpl')),
  countmoney: ParseTemplate(require('./tpl/countmoney.xtpl'))
}
var Main = ZFT.Util.Class({
  container: $('#countBox'),
  init: function () {
    var _this = this;
    _this.loadCountBox();
    _this.loadCountMoneyBox(); 
    $("#getSumInp").on("change",function(e){
      _this.loadCountMoneyBox(e);
    })
  },
  loadCountBox: function () {
    var _this = this,
      container = $("#countBuilding"),
      html = '';
    Ajax('/r/Building_incomeBuildSummary', {
      type: 'POST',
      dataType: 'json',
      loading: function () {
        var loading = '<span class="z-t-a-c i-b p100">加载中，请稍候...</span>';
        container.html(loading);
      },
      success: function (res) {
        if (res.code == 200) {
          html = Template.count({ data: res.data });
          container.html(html);
        } else {
          var error = '<span class="z-t-a-c i-b p100">' + res.msg + '</span>';
          container.html(error);
        }
      }
    })
  },
  loadCountMoneyBox: function (e) {
    var _this = this,
      countMoney = $('#countMoney'),
      newdate=(e) ? $(e.target).val(): "",
      date = new Date(),
      year = date.getFullYear(),
      m = date.getMonth() + 1 + "",
      month = (m.length <= 1) ? "0" + m : m,
      ym = year + month;
    Ajax("/r/UserCenter_moneySummary", {
      type: "POST",
      dataType: "json",
      params: {
        time: (newdate) ? newdate : ym
      },
      loading: function () {
        var loading = '<span class="z-t-a-c i-b p100">加载中，请稍候...</span>';
        countMoney.html(loading);
      },
      success: function (res) {
        if (res.code == 200) {
          html = Template.countmoney({ data: res.data });
          countMoney.html(html);
          _this.initDatePicker();
        } else {
          var error = '<span class="z-t-a-c i-b p100">' + res.msg + '</span>';
          countMoney.html(error);
        }
      }
    })
  },
  initDatePicker: function (ym) {
    var _this = this;
    $("#getSumInp").datepicker({
      format: "yyyymm",
      maxViewMode: 2,
      minViewMode: "months",
      language: "zh-CN",
      autoclose: true,
      todayHighlight: true,
      Orientation:'bottom right',
      
    });

  }
})
$(function () {
  new Main()
})
